import { MessageBox, Message,Loading } from 'element-ui'
import request from './request';
import router from 'vue-router';
export default {
  isLoading:undefined,
  alert(info = '操作失败'){
    MessageBox.alert(info,'操作提示', {
      confirmButtonText: '确定'
    });
  },
  location(url = ''){
    console.log('执行到了111');
    //router.replace({ name: "login", path: "/login" });
    //router.push({name:'login',path:'/login'});
    //router.push('/login');
    console.lofg('执行到了222')
  },
  toast(info = '操作错误',url = ''){
    Message({message:info});
    if (url) router.push(url);
  },
  warnToast(info = '操作成功',url = ''){
    Message({message:info,type:'warning'});
    if (url) router.push(url);
  },
  succToast(info = '操作成功',url = ''){
    Message({message:info,type:'success'});
    if (url) router.push(url);
  },
  errorToast(info = '操作失败',url = ''){
    Message({message:info,type:'error'});
    if (url) router.push(url);
  },
  delConfirm(fn,notice = true,info = '此操作将永久删除该内容, 是否继续?'){
    MessageBox.confirm(info, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      closeOnClickModal:false,
      type: 'warning'
    }).then(() => {
      if (notice) {
        Message({
          type: 'success',
          message: '删除成功!'
        });
      }
      if (typeof fn == 'function') fn();      
    }).catch(() => {
          
    });
  },
  confirm(params = ''){
    let title = '提示';
    let okText = '确定';
    let cancelText = '取消';
    let info = '';
    if (typeof(params) == 'string') {
      info = params;
    }
    return new Promise((resolve,reject)=>{
      MessageBox.confirm(info, title, {
        confirmButtonText: okText,
        cancelButtonText: cancelText,
        closeOnClickModal:false,
        type: 'warning'
      }).then(() => {
        resolve();
      }).catch(() => {
        reject();
      });
    });
    
  },
  loading(info){
      if (this.isLoading)  this.isLoading.close();
      if (info) {
          this.isLoading = Loading.service({
              lock: true,
              text: info,
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
          });
      } 
  },
  request(params){
    return request(params);
  }
}